define(['jquery', 'static', 'underscore', 'common', 'layui', 'Home', 'Area', 'User', 'Route', 'Map', 'Task', 'Examine', 'Repair', 'news', 'Report', 'suggestion', 'text!../template/nav.html'],
    function ($, static, _, T, layui, home, area, user, route, map, task, examine, repair, news, report, suggestion, tplNav) {
        var currentUserId = '';
        var currentUser = {};
        var tpls = {
            'tplNav': _.template(tplNav)
        };
        layer.config({
            path: 'js/lib/layer/'
        });
        $(function () {
            currentUserId = static.currentUser.id;
            let uRole = static.currentUser.role;
            $('#nav').html(tpls.tplNav({
                message: uRole
            }));
            $('.nav .list').click(function (event) {
                layer.closeAll();
                $('.nav .list').removeClass('selected');
                $(this).addClass('selected');
                let page = $(this).find('span').attr('name');
                setPage(page);
            });
            $('.selected').click();

            $('#config').click(function (event) {
                event.stopPropagation();
                $('.userConfig').toggle();
                Ajax("/xjrest/user/user_info?userId=" + currentUserId, function (data) {
                    currentUser = data.ret.info;
                });
            });
            $('.userConfig').click(function (event) {
                event.stopPropagation();
            });
            $('body').click(function (event) {
                $('.userConfig').hide();
            });

            $('#setUserAccount').click(function (event) {
                $('.userConfig').hide();
                let man = {
                    1: 'selected',
                    2: ''
                };
                let woman = {
                    1: '',
                    2: 'selected'
                };
                let _html = `<div class="userConfigBody">
                            <div class='configRow'>
                                <p class="configItem">设置用户名：</p>
                                <input class="input" id="userNameConfig" type="text" value="${currentUser.userName}" placeholder="请输入用户名" />
                            </div>
                            <div class='configRow'>
                                <p class="configItem">设置电话号码：</p>
                                <input class="input" id="phone" type="text" value="${currentUser.phone}" placeholder="请输入电话号码" oninput="value=value.replace(/[^\\d]/g,'')" />
                            </div>
                            <div class='configRow' style="justify-content: center">
                                <input type="button" class="configIn buttonBlue" id="configIn" value="提交设置" />
                            </div>
                            <img src="img/user/6904332C-0AF0-402E-B315-6EE99B1DC08B@3x.png" class="closeInfo" id="closeInfo">
                        </div>`;
                layer.open({
                    type: 1,
                    title: false,
                    closeBtn: 0,
                    shadeClose: true,
                    skin: 'userConfigAccount',
                    content: _html
                });
                $("#closeInfo").click(function (event) {
                    $('.layui-layer-shade').click();
                });
                $("#configIn").click(function (event) {
                    layer.confirm('确定提交？', {
                        btn: ['确定', '取消']
                    }, function () {
                        layer.closeAll();
                        saveUserConfig();
                    }, function () {

                    });
                });
            });

            $('#setUserPassword').click(function (event) {
                $('.userConfig').hide();
                let _html = `<div class="userConfigBody">
                            <div class='configRow'>
                                <p class="configItem">输入原密码：</p>
                                <input class="input" id="oldPassword" type="password" placeholder="请输入原密码" />
                            </div>
                            <div class='configRow'>
                                <p class="configItem">设置新密码：</p>
                                <input class="input" id="setPasssword" type="password" placeholder="请输入新密码" />
                            </div>
                            <div class='configRow'>
                                <p class="configItem">确认新密码：</p>
                                <input class="input" id="newPassword" type="password" placeholder="请再次输入新密码" />
                            </div>
                            <div class='configRow' style="justify-content: center">
                                <input type="button" class="configIn buttonBlue" id="configIn" value="提交设置" />
                            </div>
                            <img src="img/user/6904332C-0AF0-402E-B315-6EE99B1DC08B@3x.png" class="closeInfo" id="closeInfo">
                        </div>`;
                layer.open({
                    type: 1,
                    title: false,
                    closeBtn: 0,
                    shadeClose: true,
                    skin: 'userConfigAccount',
                    content: _html
                });

                $("#closeInfo").click(function (event) {
                    $('.layui-layer-shade').click();
                });
                checkBlur('.userConfigBody');
                $('#newPassword').blur(function (event) {
                    if ($(this).val() != $("#setPasssword").val()) {
                        layer.msg('新密码输入不一致!');
                    }
                });
                $("#configIn").click(function (event) {
                    if (checkInput('.userConfigBody') == 1) {
                        layer.msg('请输入完整信息');
                        return;
                    }
                    layer.confirm('确定提交？', {
                        btn: ['确定', '取消']
                    }, function () {
                        layer.closeAll();
                        saveUserPassword();
                    }, function () {

                    });
                });
            });

            $('#logOut').click(function (event) {
                layer.confirm('确定退出？', {
                    btn: ['确定', '取消']
                }, function () {
                    layer.closeAll();
                    logOut();
                }, function () {

                });
            });
        })

        function saveUserConfig() {
            let parms = {};
            parms.userId = currentUserId;
            parms.gender = $("#gender").val();
            parms.username = $("#userNameConfig").val();
            parms.phone = $("#phone").val();
            Ajax("/xjrest/user/user_update", saveUserBack, parms);
        }

        function saveUserPassword() {
            let parms = {};
            parms.userId = currentUserId;
            parms.oldPassword = $("#oldPassword").val();
            Ajax("/xjrest/user/user_password", function (data) {
                if (data.ret.message == "输入旧密码正确！") {
                    let parm = {};
                    parm.userId = currentUserId;
                    parm.password = $("#newPassword").val();
                    Ajax("/xjrest/user/user_update", saveUserBack, parm);
                } else {
                    layer.msg(data.ret.message)
                }
            }, parms);
        }

        function saveUserBack(data) {
            if (data.ret.success) {
                layer.msg('设置成功');
            } else {
                layer.msg('设置失败');
            }
        }

        function setPage(page) {
            $('#content').html('').animate({
                scrollTop: 0
            }, 0);
            if (page == 'home') {
                home.show();
            } else if (page == 'area') {
                area.show();
            } else if (page == 'user') {
                user.show();
            } else if (page == 'route') {
                route.show();
            } else if (page == 'map') {
                map.show();
            } else if (page == 'task') {
                task.show();
            } else if (page == 'examine') {
                static.currentUser.role == 1 ? examine.show(0) : examine.show(1);
            } else if (page == 'repair') {
                repair.show();
            } else if (page == 'news') {
                news.show();
            } else if (page == 'report') {
                report.show();
            } else if (page == 'suggestion') {
                suggestion.show();
            }
        }
    })